﻿var workUtils = {
    params: {},
    createTable: function (json) {
        var index = layer.load(1);
        var params = json.params || {};
        params["pageNo"] = json["pageNo"];
        params["pageSize"] = json["pageSize"];
        $.ajax({
            url: json["url"],
            async: false,
            type: 'post',
            dateType: 'json',
            data: params,
            success: function (result) {
                var obj = result;
                var count = obj.count;
                var data = obj.list;
                layer.close(index);
                //计算总页数（一般由后台返回）
                json.render(data);
                json.laypage.render({
                    elem: json["laypageId"]
                    , count: count
                    , limit: json["pageSize"]
                    , page: json["pageNo"]
                    , layout: ['count', 'prev', 'page', 'next', 'limit', 'skip']
                    , jump: function (obj, first) {
                        //首次不执行
                        if (!first) {
                            json["pageNo"] = obj.curr;
                            json["pageSize"] = obj.limit;
                            tableUtils.create(json);
                        }
                    }
                });
            }
        });
        return this;
    },
    toDate: function (time, format) {
        format = format || "yyyy-MM-dd";
        return new Date(time).Format(format);
    },
    parseParams:function(){
        var param = location.search;
        param = param.substring(1,param.length);
        var json = {};
        var strs = param.split("&");
        for(var i=0;i<strs.length;i++){
            json[strs[i].split("=")[0]] = strs[i].split("=")[1];
        }
        return json;
    }
    ,
    getNowDate: function (format) {
        var time = new Date().getTime();
        format = format || "yyyy-MM-dd";
        return new Date(time).Format(format);
    },
    open: function (json, callback) {
        json["width"] = json["width"] || "480px";
        json["height"] = json["height"] || "470px";
        json["offset"] = json["offset"] || "auto";
        $.get(json["url"], function (result) {
            layer.open({
                type: 1,
                title: json["title"] || "",
                area: [json["width"], json["height"]],
                anim: 2,
                offset: json["offset"],
                skin: 'pm-layer-login',
                content: result
                ,zIndex: layer.zIndex //重点1
                ,success: function(layero){
                    layer.setTop(layero); //重点2
                }
                ,end: function (index) {
                    if (callback) {
                        callback();
                    }
                }
            });
        });
    },
    popup: function (json, callback) {
        json["width"] = json["width"] || "480px";
        json["height"] = json["height"] || "470px";
        $.get(json["url"], function (result) {
            layer.open({
                id: "layer-msg",
                type: 1,
                area: [json["width"], json["height"]],
                anim: 1,
                skin: 'pm-layer-login',
                content: result,
                zIndex: layer.zIndex, //重点1
                end: function (index) {
                    if (callback) {
                        callback();
                    }
                }
            });
        });
    },
    html: function (json) {
        var id = json["id"] || "layui-body";
        $.ajax({
            type: 'get',
            url: json["url"],
            dataType: "html",
            success: function (result) {
                $("#" + id).html(result);
            }
        });
    },
    fillForm: function ($form, json) {
        var jsonObj = json;
        if (typeof json === 'string') {
            jsonObj = $.parseJSON(json);
        }

        for (var key in jsonObj) {  //遍历json字符串
            var str = jsonObj[key];
            var tagobjs = $("[name=" + key + "]", $form);
            if ($(tagobjs[0]).attr("type") == "radio") {//如果是radio控件
                $.each(tagobjs, function (keyobj, value) {
                    if ($(value).attr("val") == jsonObj[key]) {
                        value.checked = true;
                    }
                });
                continue;
            }
            $("[name=" + key + "]", $form).val(jsonObj[key]);

        }
    },
    clearForm: function ($form, cols) {
        for (var i = 0; i < cols.length; i++) {  //遍历json字符串
            $("[name=" + cols[i] + "]", $form).val("");
        }
    }
    ,
    setCkb: function (name, value) {
        //alert(name + " " + value);
        //$("[name=" + name + "][value=" + value + "]").attr("checked", "checked");  不知为何找不到具体标签;
        $("[name=" + name + "][val=" + value + "]").attr("checked", "checked");
    },
    buildMenu: function (id, menus) {
        var html = "";
        html += '<div class="layui-col-md6 layui-table-cell menu-bars">';
        for (var key in menus) {
            html += '<a  data-id="' + id + '" class="layui-btn layui-btn-xs action-' + key + '">' + menus[key] + '</a> ';
        }
        html += '</div>';
        return html;
    },
    buildMenu1: function (id, menus) {
        var html = "";
        html += '<div class="layui-col-md4  menu-bars">';
        html += '<i class="layui-icon layui-icon-menu" style="font-size: 30px; color: #1E9FFF;">&#xe65f;</i>  ';
        html += '<dl class="widget-menu">';
        for (var key in menus) {
            if (key == 'split') {
                html += '<dd  class="action-split"></dd> ';
            } else {
                html += '<dd  data-id="' + id + '" class="action-' + key + '">' + menus[key] + '</dd> ';
            }

        }
        html += '</dl></div>';
        return html;
    },
    put: function (key, value) {
        this.params[key] = value;
    },
    get: function (key) {
        var value = this.params[key];
        return value;
    },
    msg: function (result, callback) {
        layer.msg(result.msg, {time: 1000,zIndex: layer.zIndex},function () {
            if (result.status) {
                layer.closeAll();
            }
            if (callback) {
                callback();
            }
        });
    },
    ajaxGet: function (url, data, callback) {
        $.ajax({
            url: url,
            async: false,
            type: 'get',
            dateType: 'json',
            data: data,
            success: function (data) {
                if(callback)
                    callback(data);
            }
        });
    },
    ajaxPost: function (url, data, callback, msg) {
        var func = function () {
            $.ajax({
                url: url,
                async: false,
                type: 'post',
                dateType: 'json',
                data: data,
                success: function (data) {
                    if(callback)
                        callback(data);
                }
            });
        };
        if (msg == undefined) {
            func();
        } else {
            layer.confirm(msg, {
                btn: ['确定', '取消'] //可以无限个按钮
                ,zIndex: layer.zIndex //重点1
            }, function (index, layero) {
                func();
            });
        }
    },
    delete: function (url, callback, msg) {
        layer.confirm(msg || '确定执行删除操作吗？', {
            btn: ['确定', '取消'] //可以无限个按钮
            ,zIndex: layer.zIndex //重点1
        }, function (index, layero) {
            $.ajax({
                url: url,
                async: false,
                type: 'post',
                dateType: 'json',
                success: function (result) {
                    layer.msg(result.msg, {time: 1000});
                    if (callback != undefined) {
                        callback(result);
                    }
                }
            });
        }, function (index) {
        });
    },
    height: function (id, hei) {

    },
    tableHeight: function (id, hei) {
        var obj = $(id).next().find(".layui-table-body");
        obj.height(obj.height() + hei);
    },
    toggle: function (id, cla, obj) {
        $(id).removeClass(cla);
        $(obj).addClass(cla);
    },
    onclick:function (id,callback) {
        $(document).off("click",id);
        $(document).on("click", id, function (event) {
            event.stopPropagation();
            if(callback){
                callback(this);
            }
        });
    }
};
/*
 js由毫秒数得到年月日
 */
Date.prototype.Format = function (fmt) { //author: meizz
    var o = {
        "M+": this.getMonth() + 1, //月份
        "d+": this.getDate(), //日
        "h+": this.getHours(), //小时
        "m+": this.getMinutes(), //分
        "s+": this.getSeconds(), //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
};

$(document).on("click", function (e) {
    var target = $(e.target);
    if (target.closest(".widget-menu").length == 0) {
        $(".widget-menu").hide();
    }
});
